<?php namespace Phpcmf\Model\Duanxin;

use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use Overtrue\EasySms\Strategies\OrderStrategy;
use Phpcmf\Model;
use Phpcmf\Service;

class Sms extends Model
{
    private array $myConfig;

    private array $data;

    public function __construct()
    {
        $this->data = Service::M('app')->get_config('Duanxin');

        $this->myConfig = [
            // HTTP 请求的超时时间（秒）
            'timeout' => 5.0,
            // 默认发送配置
            'default' => [
                // 网关调用策略，默认：顺序调用
                'strategy' => OrderStrategy::class,
                // 默认可用的发送网关
                'gateways' => [
                    'qcloud', 'aliyun', 'smsbao'
                ],
            ],
            // 可用的网关配置
            'gateways' => [
                // 腾讯云
                'qcloud' => [
                    'sdk_app_id' => $this->data['qcloud']['sdk_app_id'], // 短信应用的 SDK APP ID
                    'secret_id' => $this->data['qcloud']['secret_id'], // SECRET ID
                    'secret_key' => $this->data['qcloud']['secret_key'], // SECRET KEY
                    'sign_name' => $this->data['sign_name'], // 短信签名
                ],
                // 阿里云
                'aliyun' => [
                    'access_key_id' => $this->data['aliyun']['access_key_id'], // ACCESS_KEY_ID
                    'access_key_secret' => $this->data['aliyun']['access_key_secret'], // ACCESS_KEY_SECRET
                    'sign_name' => $this->data['sign_name'], // 短信签名
                    'template_id' =>$this->data['aliyun']['template_id'], 
                ],
                // 短信宝
                'smsbao' => [
                    'user' => $this->data['smsbao']['user'], // 账号
                    'password' => $this->data['smsbao']['password'] // API KEY
                ],
            ],
        ];
    }

    // 发送验证码
    public function send_code($mobile, $code, $config)
    {
        $easySms = new EasySms($this->myConfig);
        try {
            $easySms->send($mobile, [
                'content' => function ($gateway) use ($code) {
                    if ($gateway->getName() == 'aliyun') {
                        return '您的验证码为：${code}';
                    } elseif ($gateway->getName() == 'smsbao') {
                        return '【' . $this->data['sign_name'] . '】您的验证码为：' . $code;
                    }
                    return '您的验证码为：' . $code;
                },
                'template' => function ($gateway) {
                    if ($gateway->getName() == 'qcloud') {
                        return $this->data['qcloud']['template_id'];
                    } elseif ($gateway->getName() == 'aliyun') {
                        return $this->data['aliyun']['template_id'];
                    }
                    return '';
                },
                'data' => function ($gateway) use ($code) {
                    if ($gateway->getName() == 'qcloud') {
                        return [
                            $code
                        ];
                    }
                    return [
                        'code' => $code
                    ];
                },
            ]);
            return dr_return_data(1, 'ok');
        } catch (InvalidArgumentException|NoGatewayAvailableException $e) {
            return dr_return_data(0, $e->getMessage());
        }
    }

    // 发送短信
    function send_text($mobile, $content, $config)
    {
        $easySms = new EasySms($this->myConfig);
        try {
            $easySms->send($mobile, [
                'content' => function ($gateway) use ($content) {
                    if ($gateway->getName() == 'smsbao') {
                        return '【' . $this->data['sign_name'] . '】' . $content;
                    }
                    return $content;
                },
                'template' => function ($gateway) use ($content) {
                    return $content;
                },
                'data' => [],
            ]);
            return dr_return_data(1, 'ok');
        } catch (InvalidArgumentException|NoGatewayAvailableException $e) {
            return dr_return_data(0, $e->getMessage());
        }
    }
}
